﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Nirvana
{
    public static class DirectoryUtil
    {

        public static string RelativePath(string directory, string file)
        {
            return RelativePath(new DirectoryInfo(directory), new FileInfo(file));
        }

        public static string RelativePath(DirectoryInfo dirInfo,FileInfo fileInfo)
        {
            string dirName = dirInfo.FullName;
            string fileName = fileInfo.FullName;
            if (!fileName.StartsWith(dirName))
            {
                Debug.Log("AssetImportRule配置文件不和资源处于同一父目录或同一目录下");
            }
            string file = fileName.Substring(dirName.Length + 1);
            return file.Replace('\\', '/');
        }

        public static bool IsSubPath(string file,string dir)
        {
            return IsSubPath(new DirectoryInfo(dir),new FileInfo(file));
        }

        public static bool IsSubPath(DirectoryInfo dirInfo, FileInfo fileInfo)
        {
            string dirName = dirInfo.FullName;
            string fileName = fileInfo.FullName;
            if (!fileName.StartsWith(dirName))
            {
                return false;
            }

            if (fileName.Length == dirName.Length) return false;

            char c = fileName[dirName.Length];
            //确认目录分隔符
            return c == Path.DirectorySeparatorChar || c == Path.AltDirectorySeparatorChar;

        }

        public static long GetDirectoryFileSize(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                return 0L;
            }
            long totalSize = 0L;
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            foreach (FileInfo fileInfo in directoryInfo.GetFiles())
            {
                totalSize += fileInfo.Length;
            }
            DirectoryInfo[] directories = directoryInfo.GetDirectories();
            if (directories.Length > 0)
            {
                for (int j = 0; j < directories.Length; j++)
                {
                    totalSize += GetDirectoryFileSize(directories[j].FullName);
                }
            }
            return totalSize;
        }

    }
}

